/*
 * Copyright (C) 2024, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: your-name <your-email@kylinos.cn>
 *
 */
#include "timer.h"

#include <QDebug>

static Timer* m_instance = nullptr;
Timer *Timer::instance()
{
    if (!m_instance) {
        m_instance = new Timer();
    }
    return m_instance;
}

Timer::Timer(QObject *parent)
    : QObject(parent), m_timer(new QTimer(this)), m_elapsedTime(0)
{
    connect(m_timer, &QTimer::timeout, this, &Timer::updateElapsedTime);
}

Timer::~Timer()
{
    if (m_timer != nullptr) {
        if (m_timer->isActive()) {
            m_timer->stop();
        }
        delete m_timer;
        m_timer = nullptr;
    }
}

void Timer::start()
{
    m_timer->start(1000);
}

void Timer::stop()
{
    if (m_timer->isActive()) {
        m_timer->stop();
    }
}

int Timer::elapsedTime()
{
    return m_elapsedTime;
}

void Timer::updateElapsedTime()
{
    m_elapsedTime++;
    Q_EMIT elapsedTimeChanged();
}